
var lefttype = "";
(function(win,$){
	if (!('placeholder' in document.createElement('input'))) {
		$('input[placeholder],textarea[placeholder]').each(function() {
			var that = $(this),
				text = that.attr('placeholder');
			if (that.val() === "") {
				that.val(text).addClass('placeholder');
			}
			that.focus(function() {
					if (that.val() === text) {
						that.val("").removeClass('placeholder');
					}
				})
				.blur(function() {
					if (that.val() === "") {
						that.val(text).addClass('placeholder');
					}
				})
		});
	}
	
	var $policylist = $("#policylist"),
        $pager = $(".pager"),
        $txtsearch = $("#txtsearch"),
        $btnsearch = $("#btnsearch"),
        $hotpolicy = $("#hotpolicy");

    // var policylistTmpl = Util.clearHtml($("#policylist-tmpl").html());

    var M = Mustache;

    // M.parse(policylistTmpl);

    //左侧
    function tableData(pageindex, pagesize) {
        var pms = {
            pageindex: pageindex + 1,
            pagesize: pagesize
        };
        // $policylist.empty();
        var policyName = $("#txtsearch").val();
        var policyLevel = $('.ewb-info-radio input:radio:checked').val();
        var policyType = lefttype;
        pms.userGuid = $.cookie('the_userid'); //获取 用户唯一标识
        $.ajax({
            url: Util.AjaxRUrl + 'getPolicy',
            type: "post",
            dataType: "json",
            data: {
                param: '{"policyType":"' + policyType + '","policyLevel":"' + policyLevel + '","policyName":"' + policyName + '","pageIndex":"' + pms.pageindex + '","pageSize":"' + pms.pagesize + '"}'
            },
            success: function(data) {
				if(data.PolicyInfo){
					for(var i=0;i<data.PolicyInfo.length;i++){
						if(data.PolicyInfo[i].status=="申报已截止"){
							data.PolicyInfo[i].newstatus = "1";
						}else if(data.PolicyInfo[i].status=="申报进行中"){
							data.PolicyInfo[i].newstatus = "2";
						}else{
							data.PolicyInfo[i].newstatus = "3";
						}
						
					}
				}
                renderList(data.PolicyInfo);
                renderPager(pageindex, pms.pagesize, data.totalNum.policyTotal);
            },
            error: function(XMLHttpRequest, txtStatus, errorThrown) {
                console.log(XMLHttpRequest);
            }
        });
    }

    function renderList(data) {
        $policylist.html(M.render(policylistTmpl, { items: data }));
        $("#policylist").find("tr").eq(0).addClass("top");
    }

    function renderPager(pageindex, pagesize, total) {
        if ($pager.pagination()) {
            $pager.pagination('destroy');
        }

        $pager.pagination({
            pageIndex: pageindex,
            pageSize: pagesize,
            total: total,
            showJump: true,
            jumpBtnText: 'Go'
        });

        $pager.on("pageClicked", function(event, data) {
            //分页按钮点击事件
            tableData(data.pageIndex, data.pageSize);
        });

        $pager.on("jumpClicked", function(event, data) {
            //跳转按钮点击事件
            tableData(data.pageIndex, data.pageSize);
        });

    }

    //首次加载
    tableData(0, 12);

    //搜索
    $btnsearch.on('click', function(event) {

        tableData(0, 12);

    });

    //六个分类选中
    $(".ewb-menu-item").on("click", function() {
		lefttype = $(this).find("a").attr("value");
        tableData(0, 12);
    });
	
	$(":radio").on("click", function() {
        tableData(0, 12);
    });

}(this,jQuery));